package I;

import I.User;

import java.util.InputMismatchException;
import java.util.Scanner;

public class ATM{
    public static User[] setUser(){
        User[] arrays = new User[5];
        User Users1 = new User("aaaaaa","111111",1000);
        User Users2 = new User("bbbbbb","222222",2000);
        User Users3 = new User("cccccc","333333",3000);
        User Users4 = new User("dddddd","444444",4000);
        User Users5 = new User("eeeeee","555555",5000);
        arrays[0] = Users1;
        arrays[1] = Users2;
        arrays[2] = Users3;
        arrays[3] = Users4;
        arrays[4] = Users5;
        return arrays;
    }
    public static void main(String[] args) {
        process();
    }
    static User[] user = setUser();
    static int index = 0;
    static Scanner sc = new Scanner(System.in);

    //控制整个流程的方法
    public static void process(){
        System.out.println("欢迎使用lovo银行");
        while (true){
            login();
            menu();
        }

    }

    public static void login(){
        for (int i = 0;i < 3;i++ ){
            System.out.println("请输入账号：");
            String account = sc.next();
            System.out.println("请输入密码：");
            String pwd = sc .next();
            for (int j = 0;j< user.length;j++){
                if (user[j].getAccount().equals(account)&&user[j].getPwd().equals(pwd)){
                    System.out.println("登录成功");
                    index = j;
                    return;
                }
            }
            System.out.println("账号或密码错误，请重新输入，还剩"+(2-i)+"次");
        }
        System.out.println("次数已用完，卡已被吞");
        System.exit(0);//系统结束

    }

    public static void menu(){
        while (true){
            System.out.println("请选择：1-查询 2-存款 3-取款 4-转账 5-切换账号 6-退出 7-修改密码");
            int input = 0;
            try {
                input = sc.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入有误");
                sc.next();
                continue;
            }
            if(input == 1){
                search();
            }else if (input==2){
                save();
            }else if (input==3){
                get();
            }else if (input==4){
                transfer();
            }else if (input==5){
                System.out.println("账号已退出");
                return;
            }else if (input==6){
                System.out.println("谢谢使用");
                System.exit(0);
            }else if (input==7){
                if(changePwd()){
                   System.out.println("已登出");
                    return;
                }
            }
            else {
                System.out.println("选择有误，请重新选择");
            }
        }
    }
    //存款
    public static void save(){
        while (true){System.out.println("请输入存入的金额：");
        double money =  0;
        try{
             money = sc.nextDouble();
        }catch (InputMismatchException e){
            System.out.println("输入有误,请重新输入");
            sc.next();
            continue;
        }
            user[index].setBlance(user[index].getBlance() + money);
            System.out.println("存款成功,您的余额为："+user[index].getBlance());
    }
    }

    //取款
    public static void get() {
        while (true) {
            System.out.println("请输入取款金额：");
            double money = 0;
            try {
                money = sc.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("输入有误，请重新输入");
                sc.next();
                continue;
            }
            if (money <= user[index].getBlance()) {
                user[index].setBlance(user[index].getBlance() - money);
                System.out.println("取款成功，您的余额为：" + user[index].getBlance());
            } else {
                System.out.println("您的余额不足");
            }
        }
    }
    //查询
    public static void search(){
        System.out.println("您的余额为："+user[index].getBlance());
    }
    //转账
    public static void transfer(){
        while (true){System.out.println("请输入转账金额：");
        double money = 0;
        try {
             money = sc.nextDouble();
            System.out.println("输入的账号不存在");
        }catch (InputMismatchException e){
            System.out.println("输入有误，请重新输入");
            sc.next();
            continue;
        }
            // 判断余额是否足够
            if(money > user[index].getBlance()){
                System.out.println("余额不足");
                return;
            }
            System.out.println("请输入对方账号：");
            String otherAccount = sc.next();
            // 不能转给自己
            if(otherAccount.equals(user[index].getAccount())){
                System.out.println("不能转给自己");
                return;
            }
            // 循环判断输入的账号是否存在
            for(int i = 0;i < user.length;i++){
                if(user[i].getAccount().equals(otherAccount)){
                    // 当前账户的钱扣去
                    user[index].setBlance(user[index].getBlance() - money);
                    // 把钱加到对方账户上
                    user[i].setBlance(user[i].getBlance() + money );
                    System.out.println("转账成功");
                    return;
                }
            }
        }
    }
    //更改账户密码
    public static boolean changePwd(){
        System.out.println("请输入原密码：");
        String oldPwd = sc.next();
        if(!oldPwd.equals(user[index].getPwd())){
            System.out.println("原密码输入错误");
            return false;
        }
        System.out.println("请输入新密码：");
        String newPwd = sc.next();
        if(oldPwd.equals(newPwd)){
            System.out.println("新密码不能和原密码一样");
            return false;
        }
        System.out.println("请再次输入新密码：");
        String confirmPwd = sc.next();
        if(!newPwd.equals(confirmPwd)){
            System.out.println("两次密码不一致");
            return false;
        }
        user[index].setPwd(newPwd);
        System.out.println("密码修改成功");
        return true;
    }
}
